﻿@model OT.Areas.Admin.Models.Users

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i></button>
    <h3 id="myModalLabel">Edit User</h3>
</div>
@Html.HiddenFor(model => model.UserID, new { id = "HDFID" })
<div class="modal-body">
    <div id="alert_modal">
        @Html.ValidationSummary()
    </div>

    <div class="form-horizontal" role="form">
         <div class="form-group">
            <div class="col-lg-15">
                <div id="divMsgEdit"></div>
            </div>
        </div>
        <div class="form-group" id="divFirstName">
            <label class="col-lg-3 control-label">First Name</label>
            <div class="col-lg-5">
                @Html.TextBoxFor(model => model.FirstName, new { @maxlength = "50", @class = "form-control", @placeholder = "Enter First Name" })
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
        </div>
        <div class="form-group" id="divLastName">
            <label class="col-lg-3 control-label">Last Name</label>
            <div class="col-lg-5">
                @Html.TextBoxFor(model => model.LastName, new { @maxlength = "50", @class = "form-control", @placeholder = "Enter Last Name" })
                @Html.ValidationMessageFor(model => model.LastName)
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 control-label">Address</label>
            <div class="col-lg-5">
                <textarea id="Address" class="form-control" rows="2" maxlength="100" placeholder="Enter Address">@Model.Address</textarea>
              @*   @Html.EditorFor(model => model.Address, new { @maxlength = "100", @class = "form-control", @placeholder = "Enter Address" })
                @Html.ValidationMessageFor(model => model.Address)*@
            </div>
        </div>
        <hr />
        <div class="form-group" style="display:none;">
            <label class="col-lg-3 control-label">Email ID</label>
            <div class="col-lg-5">
                @Html.TextBoxFor(model => model.EmailAddress, new { @maxlength = "50", @class = "form-control", @placeholder = "Enter Email ID" })
                @Html.ValidationMessageFor(model => model.EmailAddress)
            </div>
        </div>
        <div class="form-group" style="display:none;">
             <label class="col-lg-3 control-label">PassWord</label>
            <div class="col-lg-5">
                @Html.PasswordFor(model => model.Password, new { @maxlength = "20", @class = "form-control", @placeholder = "Enter Password" })
                @Html.ValidationMessageFor(model => model.Password)
            </div>
        </div>
        <div class="form-group" style="display:none;">
             <label class="col-lg-3 control-label">Re-Enter PassWord</label>
            <div class="col-lg-5">
                @Html.PasswordFor(model => model.ConfirmPassword, new { @maxlength = "20", @class = "form-control", @placeholder = "Enter ConfirmPassword" })
                @Html.ValidationMessageFor(model => model.ConfirmPassword)
            </div>
        </div>
        <hr />
        <div class="form-group" id="divMobile">
             <label class="col-lg-3 control-label">Mobile Number</label>
            <div class="col-lg-5">
                @Html.TextBoxFor(model => model.MobileNo, new { @maxlength = "12", @class = "form-control", @placeholder = "Enter Mobile No" })
                @Html.ValidationMessageFor(model => model.MobileNo)
            </div>
        </div>
        <div class="form-group">
             <label class="col-lg-3 control-label">Landline Number</label>
            <div class="col-lg-5">
                @Html.TextBoxFor(model => model.LandlineNo, new { @maxlength = "12", @class = "form-control", @placeholder = "Enter LandlineNo No" })
                @Html.ValidationMessageFor(model => model.LandlineNo)
            </div>
        </div>
        <hr />
        <div class="form-group" id="divCountry">
            <label class="col-lg-3 control-label">Country Name</label>
            <div class="col-lg-5">
                @Html.DropDownListFor(model => model.CountryID, new SelectList(Model.Countrys, "CountryID", "CountryName"), "----Select Country---", new { id = "ddlCountry", @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CountryID)
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-3 control-label">State Name</label>
            <div class="col-lg-5">
                 @Html.DropDownListFor(model => model.StateID, new SelectList(Model.States, "StateID", "StateName"), "--select--", new { id = "ddlState" ,@class="form-control"})
                    @Html.ValidationMessageFor(model => model.StateID)
            </div>
        </div>
        <div class="form-group">
                <label class="col-lg-3 control-label">City Name</label>
                <div class="col-lg-5">
                   @Html.DropDownListFor(model => model.CityID, new SelectList(Model.Citys, "CityID", "CityName"), "--select--", new { id = "ddlCity" ,@class="form-control"})
            @Html.ValidationMessageFor(model => model.CityID)
                </div>
            </div>
        <div class="form-group" id="divUserType">
                <label class="col-lg-3 control-label">User Type</label>
                <div class="col-lg-5">
                    @Html.DropDownListFor(model => model.UserTypeID, new SelectList(Model.UserTypes, "UserTypeID", "UserType1"), "--Select User--", new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.UserTypeID)
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="form-vertical" role="form">
            <div class="form-group">
                <label class="col-lg-3 control-label">Remember me</label>
                <div class="col-lg-1">
                    @Html.EditorFor(model => model.RememberMe)
                    @Html.ValidationMessageFor(model => model.RememberMe)
                </div>
            </div>
            <div class="form-group">
                <label class="col-lg-2 control-label">Status</label>
                <div class="col-lg-1">
                     @Html.EditorFor(model => model.Status)
                </div>
            </div>
        </div>
    </div>
<div class="modal-footer">
    <button class="btn btn-default" type="submit" id="btnEdit"><i class="icon-cog"></i>Save</button>
    <button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i>Cancel</button>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $("#ddlCountry").change(function () {
            var select = $("#ddlState");
            var city = $("#ddlCity");
            city.empty();
            select.empty();
            var CountryID = $(this).val();
            if (CountryID > 0) {
                $.getJSON("/admin/user/LoadState", { CountryID: CountryID },
                       function (classesData) {
                           var select = $("#ddlState");
                           var city = $("#ddlCity");
                           city.empty();
                           select.empty();
                           select.append($('<option/>', {
                               value: 0,
                               text: "--Select State--"
                           }));
                           $.each(classesData, function (index, itemData) {

                               select.append($('<option/>', {
                                   value: itemData.Value,
                                   text: itemData.Text
                               }));
                           });
                       });
            }
        });
        $("#ddlState").change(function () {
            var StateID = $(this).val();
            $.getJSON("/admin/user/LoadCity", { StateID: StateID },
                   function (marksData) {
                       var select = $("#ddlCity");
                       select.empty();
                       select.append($('<option/>', {
                           value: 0,
                           text: "---Select City---"
                       }));
                       $.each(marksData, function (index, itemData) {
                           select.append($('<option/>', {
                               value: itemData.Value,
                               text: itemData.Text
                           }));
                       });
                   });
        });
        $("#btnEdit").click(function (e) {
            var UserID = $("#HDFID").val();
            var FirstName = $("#FirstName").val();
            var LastName = $("#LastName").val();
            var Address = $("#Address").val();
            var EmailAddress =btoa($("#EmailAddress").val());
            var Password = btoa($("#Password").val());
            var MobileNo = $("#MobileNo").val();
            var LandlineNo = $("#LandlineNo").val();
            var CountryID = $("#ddlCountry").val();
            var StateID = $("#ddlState").val();
            var CityID = $("#ddlCity").val();
            if ($("#Status").is(':checked')) {
                Status = true;
            }
            else {
                Status = false;
            }
            var RememberMe = $("#RememberMe").val();
            var UserTypeID = $("#UserTypeID").val();
            e.preventDefault();
            if (FirstName == '' || LastName == '' || MobileNo == '' || UserTypeID == 0 || CountryID == 0) {
                showMsg('divMsgEdit', 'Please Enter Value For Required Field', 'danger');
                (FirstName == '') ? $('#divFirstName').addClass('has-error') : $('#divFirstName').removeClass('has-error');
                (LastName == '') ? $('#divLastName').addClass('has-error') : $('#divLastName').removeClass('has-error');
                (MobileNo == '') ? $('#divMobile').addClass('has-error') : $('#divMobile').removeClass('has-error');
                (CountryID == 0) ? $('#divCountry').addClass('has-error') : $('#divCountry').removeClass('has-error');
                (UserTypeID == 0) ? $('#divUserType').addClass('has-error') : $('#divUserType').removeClass('has-error');
            }
            else {
                $.ajax({
                    url: "/Admin/User/Edit?UserID=" + UserID + "&FirstName=" + FirstName + "&LastName=" + LastName + "&Address=" + Address + "&EmailAddress=" + EmailAddress + "&Password=" + Password + "&MobileNo=" + MobileNo + "&LandlineNo=" + LandlineNo + "&CountryID=" + CountryID + "&StateID=" + StateID + "&CityID=" + CityID + "&Status=" + Status + "&RememberMe=" + RememberMe + "&UserTypeID=" + UserTypeID,
                    type: "POST",
                    success: function (result) {
                        if (result != "NotValid") {
                            $('#modalDiv').modal('hide');
                            showMsg('divMsgIndex', 'Record Updated Successfuly', 'success');
                            loadPage(1, 10, '');
                        }
                        else {
                            showMsg('divMsgEdit', 'Failed To Update', 'danger');
                        }
                    },
                    error: function (data) {
                    }
                });
            }
            return false;
        });
    });
</script>
